package com.springboot.rmq.comm;

import java.util.Properties;

import com.springboot.rmq.consumer.manager.MsgConsumerManager;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ConsumerConfig:
 *
 * @author MOBCB
 * @version 1.00
 * @since 2017/4/19 18:54
 */
//@Configuration
//@EnableConfigurationProperties(ConsumerProperties.class)
public class ConsumerConfig {
    /**
     * consumer配置信息
     */
    private final ConsumerProperties properties;

    /**
     * 通过构造函数，注入properties
     *
     * @param properties 配置文件属性
     */
    public ConsumerConfig(ConsumerProperties properties) {
        this.properties = properties;
    }

    /**
     * 获取consumer的配置文件信息
     *
     * @return prop
     */
    @Bean(name = "consumerManager")
    public MsgConsumerManager getConsumerProp() {
        Properties prop = new Properties();
        //设置nameserver属性
        prop.setProperty(Constant.KEY_CONSUMER_NAMESERVER, this.properties.getNameServer());
        //设置扫描文件路径
        prop.setProperty(Constant.KEY_CONSUMER_SCANPACKAGE, this.properties.getScanPackage());
        //设置默认最大获取消息条数
        prop.setProperty(Constant.KEY_CONSUMER_BATCHSIZE, this.properties.getBatchMaxSize());
        //设置默认超时时间（分钟）
        prop.setProperty(Constant.KEY_CONSUMER_TIMEOUTMINUTES, this.properties.getTimeOutMils());
        //设置最小线程数
        prop.setProperty(Constant.KEY_CONSUMER_THREADCOUNTMIN, this.properties.getThreadCountMin());
        //设置最大线程数
        prop.setProperty(Constant.KEY_CONSUMER_THREADCOUNTMAX, this.properties.getThreadCountMax());
        prop.setProperty(Constant.KEY_CONSUMER_INSTANCE, this.properties.getInstanceName());
        MsgConsumerManager consumerManager = new MsgConsumerManager();
        consumerManager.setConsumerProp(prop);
        consumerManager.initMsgHandler();
        return consumerManager;
    }
}
